package com.example.forum.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.forum.entity.TAppuser;
import com.example.forum.entity.TClickzan;
import com.example.forum.mapper.TClickzanMapper;
import com.example.forum.service.ITClickzanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author why
 * @since 2020-04-22
 */
@Service
public class TClickzanServiceImpl extends ServiceImpl<TClickzanMapper, TClickzan> implements ITClickzanService {

    @Autowired
    TClickzanMapper tClickzanMapper;

    //isIsClickZan  判断当前用户是否点赞了这个主题
    //themeID 当前用户ID
    public Boolean isClickZan(String themeId, String currentUserId) {

        Integer count = tClickzanMapper.selectCount(new QueryWrapper<TClickzan>()
                .eq("ThemeOrReplyId", themeId)
                .eq("creatorUserId", currentUserId)
                .eq("isDeleted", 0)
                .eq("themeType",0));
        if (count!=0) {
            return true;
        }
        //没有点赞
        return false;

    }

    @Override
    public Boolean clickZan(String themeId, String themeorReplyId, int themeType, TAppuser appuser) {

        TClickzan tClickzan = new TClickzan();

        tClickzan.setThemeid(themeId);
        tClickzan.setThemeorreplyid(themeorReplyId);
        tClickzan.setThemetype(themeType);
        tClickzan.setIsdeleted(false);
        tClickzan.setIsvisible(true);
        tClickzan.setCreatoruserid(appuser.getId());
        tClickzan.setCreatorname(appuser.getUsername());

        if (tClickzanMapper.insert(tClickzan)==1) {
            return true;
        }
        return false;
    }
}
